home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16929 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  74 lines

  1. Path: news.magmacom.com!not-for-mail
  2. From: ezust@mag1.magmacom.com (Acme Instant Dehydrated Boulder Kit)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is it OK to delete const *type pointers?
  5. Date: 12 Apr 1996 14:15:36 -0400
  6. Organization: Cloud-Zero, Canada
  7. Message-ID: <4km6k8$u0v@mag1.magmacom.com>
  8. References: <4k9g7c$fl9@arl-news-svc-5.compuserve.com>
  9. NNTP-Posting-Host: mag1.magmacom.com
  10.  
  11. In article <4k9g7c$fl9@arl-news-svc-5.compuserve.com>,
  12. Philippe Verdy  <100105.3120@compuserve.com> wrote:
  13. ]Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> s'Θcrit :
  14. ]> Acme Instant Dehydrated Boulder Kit wrote:
  15. ]> > 
  16. ]> > Some compilers let me do this, others do not. What I would like to know is,
  17. ]> > what does Stroustrup think? I.e. is it written somewhere in the ARM (I checked
  18. ]> > but can't find it) or in the draft standard? If somoene could e-mail me a
  19. ]> > quote or a pointer to a place where I can read where it says such a thing
  20. ]> > should or should not be allowed, I would appreciate it!
  21. ]> > 
  22. ]> > const int* array= new int[30];
  23. ]> > delete[] array;
  24. ]> > 
  25. ]> > MSVC says "can not delete const*"
  26. ]> > Some versions of G++ also don't let me do this.
  27. ]> > 
  28. ]> > Is deleting an object technically considered changing that object's value?
  29. ]> > 
  30. ]
  31. ]The following compiles under BC++ 4.0, 4.5, and 5.0:
  32. ]int const *ptr = new int(1);
  33. ]int const *arr = new int[10];
  34. ]main()
  35. ]{
  36. ]  delete ptr;
  37. ]  delete[] arr;
  38. ]}
  39. ]
  40. ]Being const does not mean the object is read-only. Nor does
  41.  
  42. Your example demonstrates something completely different from mine.
  43. I was talking about const * type, or a pointer to a constant
  44. object. you are talking about type * const...
  45.  
  46.  
  47. ]it mean that its value won't change. Nor does it mean that
  48. ]it cannot be deleted.
  49.  
  50. Deleting a type*const pointer makes total sense. i understand that.
  51.  
  52. What I didn't understand (but do now) is whether one should be able to
  53. delete an object which is pointed to by a "pointer to constant object",
  54. because i feel that deleting an object changes its value, and when one
  55. returns a pointer to a constant object from a function, the calling block
  56. should not be able to delete the object through that pointer.
  57.  
  58. But after posting to comp.lang.c++.moderated, i got a very good explanation.
  59. the gist of it was:
  60.  
  61. 1. The ARM says you can't do it.
  62. 2. The draft standard as of november 1994 says you can.
  63.  
  64. Therefore, in the future, new compilers will allow you to do this while old
  65. compilers will not. Any compiler which prevents you from deleting a const
  66. type* pointer does not conform to the draft standard.
  67.  
  68.  
  69. -- 
  70. Alan Ezust                       "Just because I work for the federal
  71. Ottawa, Canada                    government doesn't mean I'm an expert
  72. ezust@magmacom.com                on cockroaches"  -Special Agent Fox Mulder
  73. http://www2.magmacom.com/~ezust   
  74.